/*$(function () {
    $("#jqGrid").Grid({
        url: '../filetype/list',
        colModel: [
			{label: '类型名称', name: 'typeName', index: 'TYPE_NAME', width: 80,align: 'center'},
			{label: '上级类型', name: 'typePid', index: 'TYPE_PID', width: 80,align: 'center'},
			{label: '所属模块', name: 'type', index: 'TYPE', width: 80,align: 'center',
			    formatter: function (value) {
                return sysEnums.KNOW_TYPE3[value];
                }
            },
			{label: 'id', name: 'id', index: 'ID',align: 'center', key: true, hidden: true},
			{label: '备注', name: 'remark', index: 'REMARK', width: 80,align: 'center'}]
    });
});*/
$(function () {
    initialPage();
    getGrid();
});

function initialPage() {
    // $(window).resize(function () {
    //     TreeGrid.table.resetHeight({height: $(window).height() - 100});
    // });
}

function getGrid() {
    var colunms = TreeGrid.initColumn();
    var table = new TreeTable(TreeGrid.id, '../filetype/list', colunms);
    table.setExpandColumn(1);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("typePid");
    table.setExpandAll(true);
    table.setHeight($(window).height() - 100);
    table.init();
    TreeGrid.table = table;
}

var TreeGrid = {
    id: "jqGrid",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
TreeGrid.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        /*{title: '类型ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},*/
        {title: '类型名称', field: 'typeName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '上级类型', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'},
        {title: '所属模块', field: 'type', align: 'center', valign: 'middle', sortable: true, width: '100px',
            formatter: function (row) {
                return sysEnums.KNOW_TYPE3[row.type];
            }
        },
        {title: '备注', field: 'remark', align: 'center', valign: 'middle', sortable: true, width: '100px'}
    ];
    return columns;
};
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "typePid",
            rootPId: -1
        },
        key: {
            url: "nourl",
            name: "typeName" //name 与 typeName 绑定
        }
    }
};
var ztree;
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        fileType: {
            parentName: null,
            typePid: '0'
        },
        ruleValidate: {
            typeName: [
                {required: true, message: '类型名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            type:''
        },
        moduleTypeList:enumsToList('KNOW_TYPE3'), ////模块类型
    },
    methods: {
        getFault: function () {
            //加载故障类型树
            Ajax.request({
                url: "../filetype/select",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#fileTypeTree"), setting, r.fileTypeList);
                    var node = ztree.getNodeByParam("id", vm.fileType.typePid);
                    if (node != null) {
                        ztree.selectNode(node);
                        vm.fileType.parentName = node.typeName;
                    } else {
                        vm.fileType.parentName = '无上级类型';
                    }
                }
            });
        },
        typeTree: function () {
            openWindow({
                title: "选择资料类型",
                area: ['300px', '450px'],
                content: jQuery("#fileTypeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    // console.log("gggggg")
                    // console.log(vm.fileType)
                    vm.fileType.typePid = node[0].id;
                    vm.fileType.parentName = node[0].typeName;

                    layer.close(index);
                }
            });
        },

        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            var menuId = TreeGrid.table.getSelectedRow();
            var parentId = 0;
            if (menuId.length != 0) {
                parentId = menuId[0].id;
            }
            vm.fileType = {
                delFlag: '',
                typePid: parentId,
                typeName: '',
                type:'',
                id: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
                remark: '',
            };
            vm.getFault();
        },
        update: function (event) {
            var id = getFileTypeId();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.fileType = {
                delFlag: '',
                typePid: '',
                typeName: '',
                type: '',
                id: '',
                updateDate: '',
                updateUser: '',
                createUser: '',
                createDate: '',
                remark: '',
            };
            vm.getInfo(id);
            vm.getFault();
        },
        saveOrUpdate: function (event) {
            var url = vm.fileType.id == (null || '') ? "../filetype/save" : "../filetype/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.fileType),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getFileTypeId();
            if (ids == null){
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../filetype/delete/"+ids,
                    async: true,
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../filetype/info/" + id,
                async: true,
                successCallback: function (r) {
                    if (r.fileType!=null){
                        vm.fileType = r.fileType;
                    }

                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            TreeGrid.table.refresh({query:{'name': vm.q.name,'type':vm.q.type}});
          /*  vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');*/
        },
        reloadSearch: function () {
            vm.q = {
                name: '',
                type:'',
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
    }
});

function getFileTypeId() {
    var selected = $('#jqGrid').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        iview.Message.error("请选择一条记录");
        return ;
    } else {
        return selected[0].id;
    }
}
